<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.avery.entity.User"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<base href="<%=basePath%>">
<meta name="keywords" content="" />
<meta name="description" content="" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Doctaskis</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<link href="plugins/flexigrid/css/flexigrid.css" rel="stylesheet" type="text/css" />
<link href="css/documentstyle.css" rel="stylesheet" type="text/css" media="screen" />
<link href="css/jquery-ui.min.css" rel="stylesheet" type="text/css" media="screen" />
<script src="js/jquery-1.7.2.min.js"></script>
<script src="js/jquery-ui-1.10.0.custom.js"></script>
<script src="plugins/flexigrid/js/flexigrid.js"></script>
<script>
function test(){
	alert("go here1");
}
$(function() {
	var shareDocumentIds = "";
	$("input:submit,input:reset,button").button();
	//设置对话框效果
	$( "#selectFriendsDialog" ).dialog({
      autoOpen: false,
      show: {
        effect: "blind",
        duration: 1000
      },
      hide: {
        effect: "explode",
        duration: 1000
      }
    });
	function addAction(){
	  	//设置其页码为下一页
  		var shref="jsp/addDocument.jsp";
 		window.location.href=shref;
	}
	
	function editAction(com,grid){
	  	selected_count = $('.trSelected', grid).length;
		if (selected_count == 0) 
		{
			alert('请选择一条记录!');
			return;
		}
		if (selected_count > 1) 
		{
			alert('只能选择一条记录!');
			return;
		}
		names = '';
		$('.trSelected td:nth-child(2) div', grid).each(function(i) 
		{
		if (i) names += ',';
		names += $(this).text();
		});
		ids = '';
		$('.trSelected td:nth-child(1) div', grid).each(function(i) 
		{
		if (i) ids += ',';
		ids += $(this).text();
		});
		
		editDocument(ids);
	}
	
	function delAction(com,grid){
		selected_count = $('.trSelected', grid).length;
		if (selected_count == 0) 
		{
			alert('请选择文档!');
			return;
		}
		names = '';
		$('.trSelected td:nth-child(2) div', grid).each(function(i) 
		{
		if (i) names += ',';
		names += $(this).text();
		});
		ids = '';
		$('.trSelected td:nth-child(1) div', grid).each(function(i) 
		{
		if (i) ids += ',';
		ids += $(this).text();
		});
		if (confirm("确定删除文档[" + names + "]?")) 
		{
		  delDocument(ids);
		}
	}
	
	function shareAction(com,grid){
		selected_count = $('.trSelected', grid).length;
		if (selected_count == 0) 
		{
			alert('请选择文档!');
			return;
		}
		names = '';
		$('.trSelected td:nth-child(2) div', grid).each(function(i) 
		{
		if (i) names += ',';
		names += $(this).text();
		});
		ids = '';
		$('.trSelected td:nth-child(1) div', grid).each(function(i) 
		{
		if (i) ids += ',';
		ids += $(this).text();
		});
		//将需分享的文档ID存起来
		shareDocumentIds = ids;
		//显示选择好友的列表
		$( "#selectFriendsDialog" ).dialog( "open" ); 
	}
	
	function getShareUserAction(com,grid){
		selected_count = $('.trSelected', grid).length;
		if (selected_count == 0) 
		{
			alert('请选择用户分享!');
			return;
		}
		names = '';
		$('.trSelected td:nth-child(2) div', grid).each(function(i) 
		{
		if (i) names += ',';
		names += $(this).text();
		});
		ids = '';
		$('.trSelected td:nth-child(1) div', grid).each(function(i) 
		{
		if (i) ids += ',';
		ids += $(this).text();
		});
		
		if (confirm("确定分享给用户[" + names + "]?")) 
		{
		  shareDocument(ids);
		}
	}
	
	function shareDocument(ids)
	{
		$.ajax
		({
			url : 'document.do?actMethod=shareDocumentAction',
		    data : { documentids : shareDocumentIds,userids:ids},
		    type : 'POST',
		    success: function(msg){
				if(msg=="success"){
					alert("分享成功！");
					$( "#selectFriendsDialog" ).dialog( "close" );
				}else{
					alert("Oh no!error:"+msg);
				}
			},
			error: function(msg){
				alert(msg);
			}
		 });
	}
	
	function editDocument(ids)
	{
		//设置文档号并跳转
  		var shref="document.do?actMethod=getDocumentAction&documentid="+ids;
 		window.location.href=shref;
	}
	
	function delDocument(ids) 
	{
		$.ajax
		({
			url : 'document.do?actMethod=deleteDocumentAction',
		    data : { documentid : ids},
		    type : 'POST',
		    success: function(msg){
				if(msg=="success"){
					$("#documentsTable").flexReload();
				}else{
					alert("Oh no!error:"+msg);
				}
			},
			error: function(msg){
				alert(msg);
			}
		 });
	}
	
	 $('#documentsTable').flexigrid( {
		 url: 'document.do?actMethod=showAllDocument',
		dataType: 'json',
		colModel : [
			{display: '文档ID', name : 'id', width : 60, sortable : true, align: 'center'},
			{display: '文档名', name : 'documentname', width : 180, sortable : true, align: 'left'},
			{display: '文档内容', name : 'content', width : 220, sortable : true, align: 'left'},
			{display: '文档库序号', name : 'libraryid', width : 90, sortable : true, align: 'left'},
			{display: '更新时间', name : 'updatedate', width : 130, sortable : true, align: 'right'},
			{display: '创建时间', name : 'createdate', width : 130, sortable : true, align: 'right'}
			],
		buttons : [
			{name: '创建', bclass: 'add', onpress : addAction},
			{name: '查看/更改', bclass: 'delete', onpress : editAction},
			{name: '分享', bclass: 'delete', onpress : shareAction},
			{name: '删除', bclass: 'delete', onpress : delAction},
			{separator: true}
			],
		searchitems : [
			{display: 'id', name : 'id'},
			{display: 'documentname', name : 'documentname', isdefault: true}
			],
		sortname: "id",
		sortorder: "asc",
		usepager: true,
		title: 'Documents',
		useRp: true,
		rp: 2,
		showTableToggleBtn: true,
		width: 30,
		height: 200
    }); 
     $('#friendsTable').flexigrid( {
		 url: 'user.do?actMethod=showAllFriend',
		dataType: 'json',
		colNames:['用户名','邮箱','性别','年龄', '出生地','所在地'],
		colModel : [
			{display: '用户名', name : 'username', width : 60, sortable : true, align: 'center'},
			{display: '邮箱', name : 'email', width : 180, sortable : true, align: 'left'},
			{display: '年龄', name : 'age', width : 100, sortable : true, align: 'left'},
			{display: '性别', name : 'sex', width : 100, sortable : true, align: 'left'},
			{display: '出生地', name : 'birthplace', width : 220, sortable : true, align: 'left'},
			{display: '所在地', name : 'localplace', width : 220, sortable : true, align: 'left'}
			],
		buttons : [
			{name: '分享', bclass: 'add', onpress : getShareUserAction},
			{separator: true}
			],
		searchitems : [
			{display: '用户名', name : 'username',isdefault: true},
			{display: '出生地', name : 'birthplace'}
			],
		sortname: "username",
		sortorder: "asc",
		width: 30,
		height: 200
    });
});
</script>
<style type="text/css">
#documentsField{
	
	background-color: #FFF;
	width: 958px;
	margin: 0 auto;
	text-align: center;
}
#documentsField p{
	color:#000;
	text-align:left;
	padding-top:20px;
	padding-left:30px;
}
</style>
</head>
<body>
<div id="wrapper">
	<div id="header">
		<div id="logo">
			<h1><a href="#">doctaskis </a></h1>
			<p> BY AVERY HUO</p>
		</div>
			<div id="menu">
			<ul>
				<li><a href="main.do">首页</a></li>
				<li class="first current_page_item"><a href="documentLibrary.do?actMethod=showDocumentLibraryAction">文档库</a></li>
				<li><a href="jsp/TaskManagement.jsp">日程表</a></li>
				<li><a href="jsp/FriendManagement.jsp">朋友们</a></li>
				<li><a href="#">关于我</a></li>
				<li><a href="#">关于这</a></li>
			</ul>
		</div>
         <div id="userinfo">
           		<a id="userWelcome">Hi,${sessionScope.username}</a>
                <a id="signout" href="user.do?actMethod=quitUserAction">Sign out</a>
                <a id="register" href="jsp/register.jsp">Register</a>
           </div>
	</div>
	<!-- end #header -->
 
  <div id="documentsField">

	<div id="documentsTable" style=""></div>
	
	
	<!-- end #content -->
	<div style="clear: both;">&nbsp;</div>
			
	<!-- end #page -->
   </div>
   <div id="selectFriendsDialog" title="选择好友">
   		 <div id="friendsTable"></div>
   </div>
</div>
<div id="footer">
	<p>Copyright (c) 2013 Avery.com. All rights reserved. Design by Avery.</p>
</div>
<!-- end #footer -->
</body>
</html>
